package com.yjt.oauth.exception;

import com.yjt.common.vo.CommonResult;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
import org.springframework.security.oauth2.common.exceptions.InvalidTokenException;
import org.springframework.security.oauth2.common.exceptions.UnsupportedGrantTypeException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 曾智平
 * @Description: 异常处理
 * @date 2021/5/2613:27
 */
@ControllerAdvice
public class ExceptionHandles {

    @ResponseBody
    @ExceptionHandler(value = InternalAuthenticationServiceException.class)
    public CommonResult handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
        return CommonResult.error(e.getMessage());
    }
    @ResponseBody
    @ExceptionHandler(value = InvalidGrantException.class)
    public CommonResult handleInvalidGrantException(InvalidGrantException e) {
        String msg = "";
        if("坏的凭证".equals(e.getMessage())){
            msg = "用户名或密码错误";
        }else{
            msg = "刷新token已失效";
        }
        return CommonResult.error(msg);
    }

    @ResponseBody
    @ExceptionHandler(value = InvalidTokenException.class)
    public CommonResult handleInvalidTokenException(InvalidTokenException e) {
        return CommonResult.error("Token无效或过期");
    }
    @ResponseBody
    @ExceptionHandler(value = UnsupportedGrantTypeException.class)
    public CommonResult handleUnsupportedGrantTypeException(UnsupportedGrantTypeException e) {
        return CommonResult.error("不支持的授予类型");
    }


}
